﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.DataViz.NetworkDiagram.QueryControls
{
    public partial class OrdinalFilter : UserControl, IFieldFilter
    {
        public OrdinalFilter()
        {
            InitializeComponent();
        }

        #region Implementation of IFieldFilter
        private FieldStat _Stat;
        /// <summary>
        /// 
        /// </summary>
        public FieldStat Stat
        {
            get
            {
                return _Stat;
            }
            set
            {
                _Stat = value;
            }
        }

        private string _NetworkName;
        /// <summary>
        /// 
        /// </summary>
        public string NetworkName
        {
            get
            {
                return _NetworkName;
            }
            set
            {
                _NetworkName = value;
            }
        }

        public event OnFieldFilterUpdated FieldFilterUpdated;

        #endregion

        private void OrdinalFilter_Load(object sender, EventArgs e)
        {
            this.lbl_MemberName.Text = this.Stat.FieldName;
            this.clb_MemberValues.Items.Clear();

            if (this.Stat != null && this.Stat.Distributions != null)
            {
                foreach (object val in this.Stat.Distributions)
                {
                    this.clb_MemberValues.Items.Add(val.ToString());
                }
                for(int i=0;i<this.clb_MemberValues.Items.Count;i++)
                {
                    this.clb_MemberValues.SetItemChecked(i,true);
                }
            }
        }

        private void clb_MemberValues_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> allowedValues=new List<string>();
            for(int i=0;i<this.clb_MemberValues.Items.Count;i++)
            {
                if(this.clb_MemberValues.GetItemChecked(i))
                {
                    allowedValues.Add(this.clb_MemberValues.Items[i].ToString());
                }
            }

            if(this.FieldFilterUpdated !=null)
            {
                this.FieldFilterUpdated(this.NetworkName, this.Stat.FieldName, null, null, allowedValues);
            }
        }
    }
}
